package com.theyeasy.dvmini.pc.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.theyeasy.dvmini.model.Articles;
import com.theyeasy.dvmini.service.ArticleService;
import com.theyeasy.dvmini.vo.Vo_msg;

@Controller
@RequestMapping("/article")
public class ArticlesController extends BaseController {

	@Autowired
	ArticleService articleService;

	@RequestMapping("/articleIndex")
	public ModelAndView articleIndex() {
		ModelAndView mv = new ModelAndView("/pc/article/articleIndex");
		return mv;
	}
	

    @RequestMapping("/articleEdit")
    public ModelAndView articleEdit() {
        ModelAndView mv = new ModelAndView("/pc/article/articleEdit");
        return mv;
    }

	//列表
	@RequestMapping(value = "/getArticlesList")
	@ResponseBody
	public Object getArticlesList(Integer page,Integer pagesize,String keyword) {
	 
		return new Vo_msg(0,articleService.getArticlesList(page, pagesize, keyword));
	}

	// 添加
	@RequestMapping(value = "/addArticle")
	@ResponseBody
	public Object addArticle(String title,String content,String imgurl) {
		String result = articleService.addArticle(title, content, imgurl);
		if (result.equals("ok")) {
			return new Vo_msg(0,null, "添加成功");
		} else {
			return new Vo_msg(-1,null, result);
		}
	}

	// 修改
	@RequestMapping(value = "/updateArticle")
	@ResponseBody
	public Object updateArticle(Integer id,String title,String content,String imgurl) {
		String result =articleService.updateArticle(id, title, content, imgurl);
		if (result.equals("ok")) {
			return new Vo_msg(0, "修改成功");
		} else {
			return new Vo_msg(-1, result);
		}
	}

	// 删除
	@RequestMapping(value = "/deleteArticle")
	@ResponseBody
	public Object deleteArticle(Integer id) {
		String result =articleService.deleteArticle(id);
		if (result.equals("ok")) {
			return new Vo_msg(0, "删除成功");
		} else {
			return new Vo_msg(-1, result);
		}
	}

	// 获取
	@RequestMapping(value = "/getArticle")
	@ResponseBody
	public Object getArticle(Integer id) {
	Articles article =articleService.getArticle(id);
		if (article != null) {
			return new Vo_msg(0, article);
		} else {
			return new Vo_msg(-1, "获取失败，数据为空");
		}
	}
}
